package com.yimi.yinhepda.views.exceptionsUpload_module;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.DictValueAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.yimi.yinhepda.callbacks.OnClickCallBack;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.entity.bean.DictValueEntity;
import com.yimi.yinhepda.entity.response.QueryGoodsLossResponse;
import com.yimi.yinhepda.entity.response.QueryJiTuoWuResponse;
import com.yimi.yinhepda.entity.response.QueryNoTitleTypeResponse;
import com.yimi.yinhepda.entity.response.QueryPackTypeResponse;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lj
 * Date: 2019/1/7
 * Description:自定义选择框用于无头件
 */
public class CustomSelectDialog extends LinearLayout {

    private TextView tvSelectName;
    private String mTitle;
    private DictValueAdapter dictValueAdapter;
    private DictValueEntity dictValueEntity;
    private Context mContext;

    public CustomSelectDialog(Context context) {
        this(context, null);
    }

    public CustomSelectDialog(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.custom_select_dialog, this, true);
        tvSelectName = rootView.findViewById(R.id.tv_selectName);
    }

    /**
     * 查询数据字典
     *
     * @param key 数据字典key
     */
    public void queryDictValue(Class<?> object, String key, final OnClickCallBack clickCallBack) {
        Map<String, String> params = new HashMap<>();
        //移库倒货数据字典字段
        params.put("params", key);
        new NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.URL_QUERY_DICT_VALUE)
                .setParams(params)
                .setConnTimeOut(5 * 1000)
                .setResponsClazz(object)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        clickCallBack.onError(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        clickCallBack.onSuccess(obj);
                        if (obj instanceof QueryNoTitleTypeResponse) {
                            //无头件类型
                            QueryNoTitleTypeResponse response = (QueryNoTitleTypeResponse) obj;
                            setResponseMap(response.getData().getMap(), clickCallBack);
                        } else if (obj instanceof QueryJiTuoWuResponse) {
                            //寄托物
                            QueryJiTuoWuResponse response = (QueryJiTuoWuResponse) obj;
                            setResponseMap(response.getData().getMap(), clickCallBack);
                        } else if (obj instanceof QueryGoodsLossResponse) {
                            //是否泄露货
                            QueryGoodsLossResponse response = (QueryGoodsLossResponse) obj;
                            setResponseMap(response.getData().getMap(), clickCallBack);
                        } else if (obj instanceof QueryPackTypeResponse) {
                            //包装类型
                            QueryPackTypeResponse response = (QueryPackTypeResponse) obj;
                            setResponseMap(response.getData().getMap(), clickCallBack);
                        }
                    }
                });
    }

    public void setResponseMap(Map<String, String> maps, OnClickCallBack clickCallBack) {
        if (maps != null && !maps.isEmpty()) {
            List<DictValueEntity> list = new ArrayList<>();
            for (String key : maps.keySet()) {
                //将数据字典中的数据显示在话框
                DictValueEntity valueEntity = new DictValueEntity();
                if (dictValueEntity != null && dictValueEntity.getKey().equals(key)) {
                    valueEntity.setCheck(true);
                }
                valueEntity.setKey(key);
                valueEntity.setValue(maps.get(key));
                list.add(valueEntity);
            }
            showSelectDialog(mContext, list, clickCallBack);
        } else {
            //获取数据字典为空
            clickCallBack.onError("获取数据字典为空！");
        }
    }

    public void setDialogDataList(List<DictValueEntity> dataList, OnClickCallBack clickCallBack) {
        for (DictValueEntity item : dataList) {
            if (dictValueEntity != null && dictValueEntity.getKey().equals(item.getKey())) {
                item.setCheck(true);
            }
        }
        showSelectDialog(mContext, dataList, clickCallBack);
    }


    /**
     * 显示选择场地对话框
     */
    public CustomSelectDialog showSelectDialog(Context context, final List<DictValueEntity> dataList, final OnClickCallBack clickCallBack) {
        final LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.alert_dialog_select_dict_value, null);
        TextView tvTitle = layout.findViewById(R.id.tv_title);
        tvTitle.setText(mTitle);
        RecyclerView rvDataList = layout.findViewById(R.id.rv_select_item);
        Button btnOK = layout.findViewById(R.id.btn_sure);
        Button btnBack = layout.findViewById(R.id.btn_back);
        final Dialog dialog = new Dialog(context, R.style.AsyncTaskDialog);
        dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.getWindow().getDecorView().setPadding(20, 20, 20, 20);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);

        dictValueAdapter = new DictValueAdapter(context, dataList, new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                if (position == RecyclerView.NO_POSITION) {
                    return;
                }
                dictValueEntity = dataList.get(position);
                if (dictValueEntity.isCheck()) {
                    dictValueEntity.setCheck(false);
                } else {
                    for (DictValueEntity item : dataList) {
                        if (item.equals(dictValueEntity)) {
                            item.setCheck(true);
                        } else {
                            item.setCheck(false);
                        }
                    }
                }
                dictValueAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemLongClickListener(int position) {
            }
        });

        rvDataList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvDataList.setAdapter(dictValueAdapter);

        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer dictValue;
                if (dictValueEntity == null || !dictValueEntity.isCheck()) {
                    tvSelectName.setText("");
                    dictValueEntity = null;
                    dictValue = null;
                } else {
                    tvSelectName.setText(dictValueEntity.getValue());
                    dictValue = Integer.parseInt(dictValueEntity.getKey());
                }
                dialog.dismiss();
                clickCallBack.onSure(dictValue, dialog);
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                clickCallBack.onCancle();
            }
        });
        return this;
    }

    public String getTitle() {
        return mTitle;
    }

    public CustomSelectDialog setTitle(String mTitle) {
        this.mTitle = mTitle;
        return this;
    }

    public DictValueEntity getDictValueEntity() {
        return dictValueEntity;
    }

    public void setDictValueEntity(DictValueEntity dictValueEntity) {
        this.dictValueEntity = dictValueEntity;
    }

    public TextView getTvSelectName() {
        return tvSelectName;
    }

    public void setTvSelectName(TextView tvSelectName) {
        this.tvSelectName = tvSelectName;
    }
}
